package com.kaizzi.data.jdl.helper;

import com.kaizzi.data.common.been.TempBeen;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.LogicUtil;
import com.kaizzi.data.common.utils.StringUtil;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.model.Page45Model;
import com.kaizzi.data.jdl.service.OrderListService;
//import com.kaizzi.data.ppt.template.Template45;
import lombok.Data;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/16
 */
@Component
public class PageData45 {

    @Resource
    private OrderListService orderListService;

    public String getData(ExcelModel model){
        List<String> dateList = DateUtil.getDateList(1);
        List<String> transTime = DateUtil.getTransTime(dateList.get(0));

        List<TempBeen> prdLave = orderListService.getPrdLave(transTime.get(0), transTime.get(1));

        Map<String,List<TempBeen>> map = new HashMap<>();

        prdLave.stream().collect(Collectors.groupingBy(TempBeen::getTempName,Collectors.toList())).forEach(map::put);

        Page45 page45 = new Page45();
        List<TempData45> tempData45s = new ArrayList<>();
        String[] prdIds = getPrdIds();
        for (String prdId : prdIds) {

            TempData45 tempData45 = new TempData45();
            tempData45.setName(getName(prdId));
            List<TempPrd> tempPrds = new ArrayList<>();
            List<String> weekDays = DateUtil.getWeekDays(transTime.get(0));
            weekDays.forEach(day -> {

                TempPrd tempPrd = new TempPrd();
                tempPrd.setDate(day);
                tempPrd.setCount(0);
                if (map.containsKey(prdId)){
                    map.get(prdId).forEach(been -> {
                        if (been.getTempData().equals(day)){
                            tempPrd.setCount(been.getTempCount());
                            tempPrd.setDate(day);
                        }
                    });
                }
                tempPrds.add(tempPrd);
            });
            tempData45.setTempPrds(tempPrds);
            double sum = tempPrds.stream().mapToDouble(TempPrd::getCount).sum();
            tempData45.setTotal(sum);

//            double singleLave = (double)orderListService.getSingleLave(prdId, transTime.get(1));
//            double stock = getStock(prdId,model.getPage45s());
//            tempData45.setRemaining(stock);
//            String rate = "0%";
//            if (stock > 0){
//                rate = StringUtil.decimalFormat(singleLave / stock * 100)+"%";
//            }
//            tempData45.setRate(rate);

            tempData45s.add(tempData45);
        }
        page45.setData(tempData45s);
        page45.setTitle(LogicUtil.getTitle(model,45));

        return JsonUtil.serialize(page45);

    }

    private static String[] getPrdIds(){
        return new String[]{"57","58","59","60","61","62","63","64","65","66","67","68"};
    }

    @Data
    private static class TempPrd{
        private String date;
        private long count;
    }

    @Data
    private static class TempData45{
        private String name;
        private List<TempPrd> tempPrds;
        private double total;
//        private double remaining;
//        private String rate;
    }

    @Data
    private static class Page45{
        private String title;
        private List<TempData45> data;
    }


    private static String getName(String prdId){
        String name = "";
        int count = 0;
        switch (prdId){
            case "57":
                name = "小米手环5";
                break;
            case "58":
                name = "NIKE健身包";
                break;
                //20201123 篮球id 26 -> 54
            case "59":
                name = "云麦智能跳绳";
                break;
            case "60":
                name = "Keep智能体脂秤电子秤";
                break;
            case "61":
                name = "Keep健腹轮";
                break;
            case "62":
                name = "Keep泡沫轴";
                break;
            case "63":
                name = "Keep手机臂包";
                break;
            case "64":
                name = "NIKE J足球护腿板（1 对）";
                break;
            case "65":
                name = "Keep俯卧撑支架";
                break;
            case "66":
                name = "迪卡侬沙袋";
                break;
            case "67":
                name = "Keep旗舰店瑜伽砖";
                break;
            case "68":
                name = "准者膝盖冰敷贴（四片装）";
                break;
//            case "38":
//                name = "腾讯高级会员季卡";
//                break;
//            case "46":
//                name = "威尔仕健身单周+私教套餐卡";
//                break;
//            case "44":
//                name = "威尔仕单周卡";
//                break;
//            case "45":
//                name = "威尔仕健身双周卡";
//                break;
            default:
                name = "undefind";
                break;
        }
        return name;
    }
    private static double getStock(String prdId, List<Page45Model> models){
        for (Page45Model model : models) {
            if (prdId.equals(model.getId())){
                return Double.parseDouble(model.getSku());
            }
        }
        return 0;
    }
}
